package top.zimang.loveforwords.Service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;

import androidx.annotation.Nullable;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import top.zimang.loveforwords.Handler.MyHandlerSAX;
import top.zimang.loveforwords.Model.Word;
import top.zimang.loveforwords.Utils.WordUtils;

public class SearchForWords extends Service {
    private Word word;

    public Word getWord() {
        return word;
    }

    private IBinder binder=new SBinder();

    public  class  SBinder extends Binder{
        public SearchForWords getService(){
            return SearchForWords.this;
        }
    }
    public Word ParserWord(String res){
        SAXParserFactory factory=SAXParserFactory.newInstance();
        XMLReader xmlReader= null;
        MyHandlerSAX handlerSAX=new MyHandlerSAX();
        try {
            xmlReader = factory.newSAXParser().getXMLReader();
            handlerSAX.setMeans(new ArrayList<>());
            handlerSAX.setPron(new ArrayList<>());
            handlerSAX.setPs(new ArrayList<>());
            handlerSAX.setSent(new ArrayList<>());
            xmlReader.setContentHandler(handlerSAX);
            xmlReader.parse(new InputSource(new StringReader(res)));

        } catch (SAXException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Word word=null;
        if (handlerSAX.getParserWord()!=null)
            word=handlerSAX.getParserWord();
        return word;
    }
    public void sendRequestWithOkHttp(String w){
        new Thread(new Runnable() {
            @Override
            public void run() {
//                cookie
                OkHttpClient.Builder builder=new OkHttpClient.Builder();
//                还需持久化

                OkHttpClient client=builder.build();
                Request getWord=new Request.Builder()
                        .url("http://dict-co.iciba.com/api/dictionary.php?w="+w+"&key=1F9CA812CB18FFDFC95FC17E9C57A5E1")
                        .build();
                try {

                    Response resData2=client.newCall(getWord).execute();

                    String res=resData2.body().string();

                    Intent intent=new Intent();
                    word=ParserWord(res);
                    word.setSent(WordUtils.Filter(word.getSent()));
                    intent.setAction("wordReady");
                    sendBroadcast(intent);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }
}
